﻿using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XChip_Common
{
  public  class ShortCutHelper
    {    //int WindowStyle    说明
         //1    激活并显示窗口。如果该窗口被最小化或最大化，则系统将其还原到初始大小和位置。
         //3    激活窗口并将其显示为最大化窗口。
         //7    最小化窗口并激活下一个顶级窗口。

        /// <summary>
        /// 创建快捷方式
        /// </summary>
        /// <param name="directory">快捷方式所处的文件夹</param>
        /// <param name="shortcutName">快捷方式名称</param>
        /// <param name="targetPath">目标路径</param>
        /// <param name="description">描述</param>
        /// <param name="iconLocation">图标路径，格式为"可执行文件或DLL路径, 图标编号"，
        /// 例如System.Environment.SystemDirectory + "\\" + "shell32.dll, 165"</param>
        /// <remarks></remarks>
        public static void CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
        {
            if (!System.IO.Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
            shortcut.TargetPath = targetPath;//指定目标路径
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置起始位置
            shortcut.WindowStyle = 1;//设置运行方式，默认为常规窗口
            shortcut.Description = description;//设置备注
            shortcut.IconLocation = string.IsNullOrEmpty(iconLocation) ? targetPath : iconLocation;//设置图标路径   可不赋值，默认是目标图标
            shortcut.Save();//保存快捷方式
        }

        /// <summary>
        /// 创建桌面快捷方式
        /// </summary>
        /// <param name="shortcutName">快捷方式名称</param>
        /// <param name="targetPath">目标路径</param>
        /// <param name="description">描述</param>
        /// <param name="iconLocation">图标路径，格式为"可执行文件或DLL路径, 图标编号"</param>
        /// <remarks></remarks>
        public static void CreateShortcutOnDesktop(string shortcutName, string targetPath,string isUSB, string description = null, string iconLocation = null)
        {
           
            if (isUSB.Contains("USB"))
            {
                iconLocation = Application.StartupPath + "\\151ToolUSB.ico";
            }
            else if(isUSB.Contains("TVE"))
            {
                iconLocation = Application.StartupPath + "\\151ToolTVE.ico";
            }
            else if (isUSB.Contains("mipiout"))
            {
                iconLocation = Application.StartupPath + "\\151Tool.ico";
            }
            else
            {
                iconLocation = Application.StartupPath + "\\151ToolI2C.ico";
            }

            string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//获取桌面文件夹路径
            //string desktop = new DirectoryInfo(Application.StartupPath).Parent.FullName;
            CreateShortcut(desktop, shortcutName, targetPath, Application.StartupPath, iconLocation);
        }
        public static void Createashortcutontheparentpath(string shortcutName, string targetPath, string isUSB, string description = null, string iconLocation = null)
        {

            if (isUSB.Contains("USB"))
            {
                iconLocation = Application.StartupPath + "\\151ToolUSB.ico";
            }
            else if (isUSB.Contains("TVE"))
            {
                iconLocation = Application.StartupPath + "\\151ToolTVE.ico";
            }
            else if (isUSB.Contains("mipiout"))
            {
                iconLocation = Application.StartupPath + "\\151Tool.ico";
            }
            else
            {
                iconLocation = Application.StartupPath + "\\151ToolI2C.ico";
            }

            //string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//获取桌面文件夹路径
            string desktop = new DirectoryInfo(Application.StartupPath).Parent.FullName;
            CreateShortcut(desktop, shortcutName, targetPath, Application.StartupPath, iconLocation);
        }


        public static bool DoesShortcutExist(string shortcutPath)
        {
            bool shortcutExists = System.IO.File.Exists(shortcutPath);
            if (shortcutExists)
            {
                // 尝试解析快捷方式以确保它指向的文件或目录存在
                var shell = new Shell32.Shell();
                Shell32.Folder folder = shell.NameSpace(Path.GetDirectoryName(shortcutPath));
                Shell32.FolderItem folderItem = folder.ParseName(Path.GetFileName(shortcutPath));
                shortcutExists = folderItem != null;
            }
            return shortcutExists;
        }

        public static string GetShortcutTargetPath(string shortcutFilePath)
        {
            string pathOnly = Path.GetDirectoryName(shortcutFilePath);
            string fileName = Path.GetFileName(shortcutFilePath);

            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutFilePath);

            // 获取快捷方式的目标路径  
            string targetPath = shortcut.TargetPath;
            if (!string.IsNullOrEmpty(shortcut.Arguments))
            {
                targetPath += " " + shortcut.Arguments;
            }

            // 如果快捷方式指向的是另一个快捷方式，递归调用本方法获取最终的目标路径  
            if (Path.GetExtension(targetPath).ToLower() == ".lnk")
            {
                return GetShortcutTargetPath(Path.Combine(pathOnly, targetPath));
            }

            return targetPath;
        }

    }
}
